﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Scheduler;
using Microsoft.Phone.Shell;


namespace PhoneApp1
{
    public partial class NotificationsList : PhoneApplicationPage
    { IEnumerable<ScheduledNotification> notifications;
        // Constructor
    public NotificationsList()
        {
            InitializeComponent();
        }

    private void ResetItemsList()
    {
        // Use GetActions to retrieve all of the scheduled actions
        // stored for this application. The type <Reminder> is specified
        // to retrieve only Reminder objects.
        //reminders = ScheduledActionService.GetActions<Reminder>();
        notifications = ScheduledActionService.GetActions<ScheduledNotification>();

        // If there are 1 or more reminders, hide the "no reminders"
        // TextBlock. IF there are zero reminders, show the TextBlock.
        //if (reminders.Count<Reminder>() > 0)
        if (notifications.Count<ScheduledNotification>() > 0)
        {
            EmptyTextBlock.Visibility = Visibility.Collapsed;
        }
        else
        {
            EmptyTextBlock.Visibility = Visibility.Visible;
        }

        // Update the ReminderListBox with the list of reminders.
        // A full MVVM implementation can automate this step.
        NotificationListBox.ItemsSource = notifications;

        //tile

        int newCount = 0;

        // Application Tile is always the first Tile, even if it is not pinned to Start.
        ShellTile TileToFind = ShellTile.ActiveTiles.First();

        // Application should always be found
       // if (TileToFind != null)
        //{
            // if Count was not entered, then assume a value of 0
            //  if (textBoxCount.Text == "")
            //  {
            // A value of '0' means do not display the Count.
            newCount = 0;
            // }
            // otherwise, get the numerical value for Count
            /*   else
               {
                    
                   newCount = int.Parse(textBoxCount.Text);
               }*/

            // Set the properties to update for the Application Tile.
            // Empty strings for the text values and URIs will result in the property being cleared.
        try{
            StandardTileData NewTileData = new StandardTileData
            {
                Title = "OK",
                BackgroundImage = new Uri("Green.jpg", UriKind.Relative),
                Count =  notifications.ToList(), //newCount,
                BackTitle = "U",
                BackBackgroundImage = new Uri("Red.jpg", UriKind.Relative),
                BackContent = "Notifications"
            };
        }catch(Exception){}

            // Update the Application Tile
            TileToFind.Update(NewTileData);


        }
    }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            //Reset the ReminderListBox items when the page is navigated to.
            ResetItemsList();
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            // The scheduled action name is stored in the Tag property
            // of the delete button for each reminder.
            string name = (string)((Button)sender).Tag;

            // Call Remove to unregister the scheduled action with the service.
            ScheduledActionService.Remove(name);

            // Reset the ReminderListBox items
            ResetItemsList();
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            // Navigate to the AddReminder page when the add button is clicked.
            NavigationService.Navigate(new Uri("/Addreminder.xaml", UriKind.RelativeOrAbsolute));
        }

        private void backButton_Click(object sender, RoutedEventArgs e)
        {
            // Navigate to the AddReminder page when the add button is clicked.
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}